<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Random;
use think\Db;
use think\Validate;
use app\common\model\User as userModel;
use app\common\model\GoodsComment;
/**
 * 会员接口
 */
class User extends Api
{
    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 会员中心
     * @param string $token 用户token
     * @ApiReturn   ({
    "code": 200,
    "msg": "请求成功",
    "time": "1606197687",
    "data": {
    "username": "admin",用户名
    "avatar": "\/assets\/img\/avatar.png",头像
    "status_text": ""
    }
    })
     */
    public function index()
    {
        $token  = $this->request->get('token');
        $user = userModel::where('token',$token)->field('username,avatar')->find();
        $this->success($user);
    }
    /**
     * 收藏列表
     * @param string $token 用户token
     * @param string $page 页数（默认为1）
     * @ApiReturn   ({
    "code": 200,
    "msg": "请求成功",
    "time": "1606198100",
    "data": [
    {
    "id": 2,产品ID
    "name": "雷蛇机械键盘",产品名称
    "intro": "这是一个机械键盘的简介。这是一个机械键盘的简介。",产品简介
    "images": "http://www.chanpin.com/uploads/20201123/b612809cdaded818a367ec987d510786.png",产品图片
    "price": "5800.00",产品价格
    "comment": 0评价人数
    },
    {
    "id": 1,
    "name": "华为mate平板",
    "intro": "这是一个平板的简介。这是一个平板的简介。",
    "images": "http://www.chanpin.com/uploads/20201123/b612809cdaded818a367ec987d510786.png",
    "price": "5800.00",
    "comment": 1
    }
    ]
    })
     */
    public function collect_list()
    {
        $token  = $this->request->get('token');
        $user = userModel::where('token',$token)->field('id')->find();
        $page  = $this->request->get('page')==null?1:$this->request->get('page');
        $pagesize = 10;
        $page = ($page-1)*$pagesize;
        $list = Db::name('user_collect')->alias('c')->join('goods g','g.id = c.goods_id')->where('c.userid',$user['id'])->field('g.id,g.name,g.intro,g.images,g.price')->limit($page,$pagesize)->order('c.id desc')->select();
        foreach($list as &$value){
            $images = explode(',',$value['images']);
            $value['images'] = $this->domain.$images[0];
            $value['comment'] = GoodsComment::where('goods_id',$value['id'])->group('userid')->count();
        }
        $this->success($list);
    }
}
